package com.example.itshome.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.example.itshome.R;
import com.example.itshome.adapter.ActivityListPageRecycleViewAdapter;
import com.example.itshome.entity.ActivityEntity;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.GetActivityRequest;

import java.net.ResponseCache;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class ActivityItem1Fragment extends Fragment {

    private RecyclerView mRecyclerView;
    private List<ActivityEntity> mRecycleViewData;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_activity_item1, container, false);
        initFind(view);
        initRecycleView();
        return view;
    }

    void initFind(View v){
        mRecyclerView=v.findViewById(R.id.rcv_activity_list);
    }

    void initRecycleView(){
        mRecycleViewData=new ArrayList<>();
        mRecyclerView.setAdapter(new ActivityListPageRecycleViewAdapter(getContext(),mRecycleViewData).setFragment(this));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int interval = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
                outRect.left = interval/2;
                outRect.right =interval/2;
                outRect.bottom = interval;
            }
        });
        AbstractNetworkRequest request=new GetActivityRequest(getActivity())
                .setAdapter(mRecyclerView.getAdapter())
                .setCurrentData(mRecycleViewData);
        request.start();

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode==100&&data!=null&&resultCode==1){
            int id= data.getIntExtra("id",-1);
            mRecycleViewData.forEach(activityEntity -> {
                if(activityEntity.getId()==id){
                    activityEntity.setSignUp(true);
                }
            });
        }
    }
}